package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.tanhua.mytanhuadubbointerface.api.RecommendUserApi;
import cn.itcast.tanhua.mytanhuaserver.service.RecommendUserService;
import cn.itcast.tanhua.pojo.RecommendUser;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.vo.PageInfo;
import cn.itcast.tanhua.vo.TodayBest;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 郭文亮
 * @since 2021/11/26 0:16
 */
@Service
public class RecommendUserServiceImpl implements RecommendUserService {

    @Reference
    private RecommendUserApi recommendUserApi;

    @Value("${tanhua.sso.default.user}")
    private Long defaultUserId;

    @Value("${tanhua.sso.default.fateValue}")
    private Long defaultFateValue;


    @Override
    public TodayBest queryWithMaxScore(List<Long> blackUserId,Long userId) {
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(blackUserId,userId);
        TodayBest todayBest = new TodayBest();
        if (null == recommendUser) {
            // 如果没有今日佳人，那么返回默认的佳人信息
            todayBest.setId(defaultUserId);
            todayBest.setFateValue(defaultFateValue);
            return todayBest;
        }
        // 返回todayBest的id和fateValue(缘分值)
        todayBest.setId(recommendUser.getUserId());
        double fateValue = Math.floor(recommendUser.getScore());
        todayBest.setFateValue((long) fateValue);
        return todayBest;
    }

    /**
     * 调用dubbo的recommendUserApi的queryPageInfo查询推荐用户的信息
     * @param userId 当前用户id
     * @param pageNum 当前页
     * @param pageSize 每页显示的条数
     * @return pageInfo，包括pageNum，pageSize和查询到的数据
     */
    @Override
    public PageInfo<RecommendUser> queryPageInfo(List<Long> blackUserId,Long userId, Integer pageNum, Integer pageSize) {
        return this.recommendUserApi.queryPageInfo(blackUserId,userId, pageNum, pageSize);
    }

    /**
     * 查询推荐好友的缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    @Override
    public Double queryScore(Long userId, Long toUserId){
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if(ObjectUtil.isNotEmpty(score)){
            return score;
        }
        //默认值
        return 98d;
    }
}
